package com.wcl.demo.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.wcl.demo.application.BaseApplication;
import com.wcl.demo.model.entity.AroundInforDao;
import com.wcl.demo.model.entity.BackGroundData;
import com.wcl.demo.model.entity.BackGroundDataDao;
import com.wcl.demo.model.entity.CanSendDataDao;
import com.wcl.demo.model.entity.DaoMaster;
import com.wcl.demo.model.entity.DaoSession;
import com.wcl.demo.model.entity.InviteMessage;
import com.wcl.demo.model.entity.InviteMessageDao;
import com.wcl.demo.model.entity.InviteMessagesDao;
import com.wcl.demo.model.entity.NoteBgDataDao;
import com.wcl.demo.model.entity.UserDao;
import com.wcl.demo.model.entity.UserInforDao;


/**
 * Created by dllo on 15/11/20.
 */
public class DaoSingleton {
    private static final String DATABASE_NAME = "searchhim.db";

    private volatile static DaoSingleton instance;

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private UserDao userDao;
    private UserInforDao inforDao;
    private InviteMessagesDao messagesDao;
    private InviteMessageDao messageDao;
    private AroundInforDao aroundInforDao;
    private BackGroundDataDao backGroundDataDao;
    private NoteBgDataDao noteBgDataDao;
    private CanSendDataDao canSendDataDao;


    private DaoSingleton() {
        context = BaseApplication.getContext();
    }

    public static DaoSingleton getInstance() {
        return SingletonHolder.instance;
    }

    private static final class SingletonHolder {
        private static final DaoSingleton instance = new DaoSingleton();
    }

    public DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }
        return helper;
    }

    public SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    public DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }


    /***
     * 用户
     */

    public UserDao getUserDao() {

        if (userDao == null) {
            userDao = getDaoSession().getUserDao();
        }
        return userDao;
    }

    /***
     * 用户资料
     */
    public UserInforDao getInforDao() {
        if (inforDao == null) {
            inforDao = getDaoSession().getUserInforDao();
        }
        return inforDao;
    }

    /***
     * 申请预通知
     */
    public InviteMessagesDao getMessagesDao() {
        if (messagesDao == null) {
            messagesDao = getDaoSession().getInviteMessagesDao();
        }
        return messagesDao;
    }

    /***
     * 申请预通知
     */
    public InviteMessageDao getMessageDao() {
        if (messageDao == null) {
            messageDao = getDaoSession().getInviteMessageDao();
        }
        return messageDao;
    }
    /**
     * 心情笔记
     */
    public AroundInforDao getAroundInforDao(){
        if (aroundInforDao==null){
            aroundInforDao=getDaoSession().getAroundInforDao();
        }
        return aroundInforDao;
    }

    /**
     * 背景图片
     */
    public BackGroundDataDao getBackGroundDataDao(){
        if (backGroundDataDao==null){
            backGroundDataDao=getDaoSession().getBackGroundDataDao();
        }
        return backGroundDataDao;
    }
    /**
     * 心情笔记背景墙
     */
    public NoteBgDataDao getNoteBgDataDao(){
        if (noteBgDataDao==null){
            noteBgDataDao=getDaoSession().getNoteBgDataDao();
        }
        return noteBgDataDao;
    }
    /**
     * 隐私设置
     */
    public CanSendDataDao getCanSendDataDao(){
        if (canSendDataDao==null){
            canSendDataDao=getDaoSession().getCanSendDataDao();
        }
        return canSendDataDao;
    }
}
